/**
 * 
 */
package com.alphasystem.commerce.common.xml.adapter;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.alphasystem.commerce.common.xml.StringMapElement;

/**
 * @author sali
 * 
 */
public class StringMapAdapter extends
		XmlAdapter<StringMapElement[], Map<String, String>> {

	@Override
	public StringMapElement[] marshal(Map<String, String> v) throws Exception {
		StringMapElement[] elements = new StringMapElement[v.size()];
		int index = 0;
		for (Entry<String, String> entry : v.entrySet()) {
			elements[index] = new StringMapElement(entry.getKey(),
					entry.getValue());
			index++;
		}
		return elements;
	}

	@Override
	public Map<String, String> unmarshal(StringMapElement[] v) throws Exception {
		Map<String, String> map = new LinkedHashMap<String, String>();
		for (StringMapElement stringMapElement : v) {
			map.put(stringMapElement.getKey(), stringMapElement.getValue());
		}
		return map;
	}

}
